<template>
  <div class="customer-service-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">
        <i class="icon-customer-service"></i>
        TMS物流智能客服
      </h1>
      <p class="page-subtitle">为您提供专业的物流咨询和问题解决方案</p>
    </div>

    <div class="service-layout">
      <!-- 左侧提示词区域 -->
      <div class="prompt-panel">
        <div class="panel-header">
          <h3>常见问题场景</h3>
          <p>点击下方场景可快速获得解决方案</p>
        </div>

        <div class="scenario-categories">
          <!-- 异常货物状态 -->
          <div class="category-section">
            <h4 class="category-title">
              <i class="icon-warning"></i>
              异常货物状态处理
            </h4>
            <div class="scenario-list">
              <div class="scenario-item" @click="selectPrompt('货物损坏')">
                <div class="scenario-icon">📦</div>
                <div class="scenario-content">
                  <h5>货物损坏或丢失</h5>
                  <p>客户收到异常货品时的处理流程</p>
                </div>
              </div>
              <div class="scenario-item" @click="selectPrompt('服务投诉')">
                <div class="scenario-icon">💬</div>
                <div class="scenario-content">
                  <h5>服务质量投诉</h5>
                  <p>客户对服务不满意时的解决方案</p>
                </div>
              </div>
            </div>
          </div>

          <!-- 退货与物流 -->
          <div class="category-section">
            <h4 class="category-title">
              <i class="icon-return"></i>
              退货与换货处理
            </h4>
            <div class="scenario-list">
              <div class="scenario-item" @click="selectPrompt('客户退货')">
                <div class="scenario-icon">↩️</div>
                <div class="scenario-content">
                  <h5>客户退货申请</h5>
                  <p>退货地址确认和补偿处理</p>
                </div>
              </div>
              <div class="scenario-item" @click="selectPrompt('退换货')">
                <div class="scenario-icon">🔄</div>
                <div class="scenario-content">
                  <h5>商品换货服务</h5>
                  <p>退款赔偿和物流部门联系</p>
                </div>
              </div>
            </div>
          </div>

          <!-- 运输问题 -->
          <div class="category-section">
            <h4 class="category-title">
              <i class="icon-truck"></i>
              运输过程问题
            </h4>
            <div class="scenario-list">
              <div class="scenario-item" @click="selectPrompt('包装破损')">
                <div class="scenario-icon">📋</div>
                <div class="scenario-content">
                  <h5>包装破损处理</h5>
                  <p>运输中包装破损的赔偿流程</p>
                </div>
              </div>
              <div class="scenario-item" @click="selectPrompt('运输延误')">
                <div class="scenario-icon">⏰</div>
                <div class="scenario-content">
                  <h5>运输延误问题</h5>
                  <p>配送延迟的解决和补偿方案</p>
                </div>
              </div>
            </div>
          </div>

          <!-- 配送路线 -->
          <div class="category-section">
            <h4 class="category-title">
              <i class="icon-route"></i>
              配送路线管理
            </h4>
            <div class="scenario-list">
              <div class="scenario-item" @click="selectPrompt('复杂路线')">
                <div class="scenario-icon">🗺️</div>
                <div class="scenario-content">
                  <h5>复杂运输路线</h5>
                  <p>长途货运的详细物流计划</p>
                </div>
              </div>
              <div class="scenario-item" @click="selectPrompt('紧急配送')">
                <div class="scenario-icon">🚨</div>
                <div class="scenario-content">
                  <h5>紧急货物配送</h5>
                  <p>加急配送服务和路径优化</p>
                </div>
              </div>
            </div>
          </div>

          <!-- 库存管理 -->
          <div class="category-section">
            <h4 class="category-title">
              <i class="icon-warehouse"></i>
              库存管理问题
            </h4>
            <div class="scenario-list">
              <div class="scenario-item" @click="selectPrompt('库存不足')">
                <div class="scenario-icon">📊</div>
                <div class="scenario-content">
                  <h5>库存不足或积压</h5>
                  <p>补货服务和重新下单建议</p>
                </div>
              </div>
              <div class="scenario-item" @click="selectPrompt('货物损坏库存')">
                <div class="scenario-icon">⚠️</div>
                <div class="scenario-content">
                  <h5>仓储货物损坏</h5>
                  <p>库存异常检测和赔偿处理</p>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 快速操作按钮 -->
        <div class="quick-actions">
          <button class="action-btn primary" @click="clearChat">
            <i class="icon-refresh"></i>
            重新开始对话
          </button>
          <button class="action-btn secondary" @click="showHelp">
            <i class="icon-help"></i>
            使用帮助
          </button>
        </div>
      </div>

      <!-- 右侧聊天机器人区域 -->
      <div class="chat-panel">
        <div class="chat-header">
          <div class="chat-title">
            <i class="icon-robot"></i>
            智能客服助手
          </div>
          <div class="chat-status">
            <span class="status-indicator online"></span>
            在线服务中
          </div>
        </div>

        <div class="chat-container">
          <iframe
            ref="chatIframe"
            src="http://127.0.0.1/chatbot/LTcWWJYHbpKZdwy2"
            class="chat-iframe"
            frameborder="0"
            allow="microphone"
            @load="onIframeLoad"
          ></iframe>
        </div>

        <div class="chat-footer">
          <div class="service-info">
            <p><i class="icon-clock"></i> 服务时间：7×24小时在线</p>
            <p><i class="icon-phone"></i> 紧急联系：400-888-8888</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 帮助模态框 -->
    <div v-if="showHelpModal" class="modal-overlay" @click="showHelpModal = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>使用帮助</h3>
          <button class="close-btn" @click="showHelpModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="help-section">
            <h4>如何使用智能客服？</h4>
            <ul>
              <li>点击左侧场景卡片，快速获取相关问题的解决方案</li>
              <li>直接在聊天窗口输入您的问题，AI助手会为您提供专业解答</li>
              <li>支持语音输入，点击麦克风图标开始语音对话</li>
              <li>如需人工客服，请拨打400-888-8888</li>
            </ul>
          </div>
          <div class="help-section">
            <h4>常见问题类型</h4>
            <ul>
              <li><strong>货物异常：</strong>损坏、丢失、包装破损等问题</li>
              <li><strong>运输问题：</strong>延误、路线规划、配送时间等</li>
              <li><strong>退换货：</strong>退货流程、换货申请、赔偿处理</li>
              <li><strong>库存管理：</strong>库存查询、补货建议、仓储问题</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

// 响应式数据
const chatIframe = ref<HTMLIFrameElement>()
const showHelpModal = ref(false)

// 方法
const selectPrompt = (scenario: string) => {
  // 根据场景生成提示词
  const prompts: Record<string, string> = {
    '货物损坏': '我收到的货物有损坏，请问如何处理？需要什么赔偿流程？',
    '服务投诉': '我对你们的物流服务不满意，想要投诉并寻求解决方案。',
    '客户退货': '我需要退货，请问退货地址是什么？有什么补偿政策？',
    '退换货': '我想申请换货，请问流程是什么？退款如何处理？',
    '包装破损': '货物在运输过程中包装破损了，请问如何申请赔偿？',
    '运输延误': '我的货物运输延误了，请问有什么补偿方案？',
    '复杂路线': '我需要长途货运服务，请提供详细的物流计划和路线规划。',
    '紧急配送': '我有紧急货物需要配送，请问有加急服务吗？',
    '库存不足': '显示库存不足，请问什么时候能补货？如何重新下单？',
    '货物损坏库存': '仓库中的货物出现损坏，请问如何处理和申请赔偿？'
  }

  const prompt = prompts[scenario]
  if (prompt && chatIframe.value) {
    // 尝试向iframe发送消息
    try {
      chatIframe.value.contentWindow?.postMessage({
        type: 'setPrompt',
        prompt: prompt
      }, '*')
    } catch (error) {
      console.log('无法直接设置提示词，请手动输入：', prompt)
      // 可以显示一个提示框让用户复制
      navigator.clipboard?.writeText(prompt).then(() => {
        alert('提示词已复制到剪贴板，请粘贴到聊天框中')
      })
    }
  }
}

const clearChat = () => {
  if (chatIframe.value) {
    chatIframe.value.src = chatIframe.value.src // 重新加载iframe
  }
}

const showHelp = () => {
  showHelpModal.value = true
}

const onIframeLoad = () => {
  console.log('聊天机器人加载完成')
}

onMounted(() => {
  console.log('TMS物流客服页面已加载')
})
</script>

<style scoped>
.customer-service-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 30px 0;
  background: white;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.page-title {
  font-size: 2.5rem;
  color: #2c3e50;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.page-title .icon-customer-service::before {
  content: '🎧';
  font-size: 2rem;
}

.page-subtitle {
  font-size: 1.1rem;
  color: #7f8c8d;
  margin: 0;
}

.service-layout {
  display: grid;
  grid-template-columns: 400px 1fr;
  gap: 25px;
  height: calc(100vh - 200px);
}

/* 左侧提示词面板 */
.prompt-panel {
  background: white;
  border-radius: 15px;
  padding: 25px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

.panel-header {
  text-align: center;
  margin-bottom: 25px;
  padding-bottom: 20px;
  border-bottom: 2px solid #ecf0f1;
}

.panel-header h3 {
  color: #2c3e50;
  margin-bottom: 8px;
  font-size: 1.3rem;
}

.panel-header p {
  color: #7f8c8d;
  font-size: 0.9rem;
  margin: 0;
}

.category-section {
  margin-bottom: 25px;
}

.category-title {
  font-size: 1.1rem;
  color: #34495e;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid #ecf0f1;
}

.category-title .icon-warning::before { content: '⚠️'; }
.category-title .icon-return::before { content: '↩️'; }
.category-title .icon-truck::before { content: '🚛'; }
.category-title .icon-route::before { content: '🗺️'; }
.category-title .icon-warehouse::before { content: '🏪'; }

.scenario-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.scenario-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.scenario-item:hover {
  background: #e3f2fd;
  border-color: #2196f3;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.2);
}

.scenario-icon {
  font-size: 1.5rem;
  min-width: 30px;
  text-align: center;
}

.scenario-content h5 {
  margin: 0 0 4px 0;
  color: #2c3e50;
  font-size: 0.95rem;
  font-weight: 600;
}

.scenario-content p {
  margin: 0;
  color: #7f8c8d;
  font-size: 0.85rem;
  line-height: 1.4;
}

/* 快速操作按钮 */
.quick-actions {
  margin-top: 25px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.action-btn {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.action-btn.primary {
  background: #3498db;
  color: white;
}

.action-btn.primary:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.action-btn.secondary {
  background: #95a5a6;
  color: white;
}

.action-btn.secondary:hover {
  background: #7f8c8d;
  transform: translateY(-2px);
}

.action-btn .icon-refresh::before { content: '🔄'; }
.action-btn .icon-help::before { content: '❓'; }

/* 右侧聊天面板 */
.chat-panel {
  background: white;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-title {
  font-size: 1.2rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-title .icon-robot::before {
  content: '🤖';
  font-size: 1.3rem;
}

.chat-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #27ae60;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}

.chat-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.chat-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: #f8f9fa;
}

.chat-footer {
  background: #f8f9fa;
  padding: 15px 25px;
  border-top: 1px solid #ecf0f1;
}

.service-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.service-info p {
  margin: 0;
  color: #7f8c8d;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 5px;
}

.service-info .icon-clock::before { content: '🕐'; }
.service-info .icon-phone::before { content: '📞'; }

/* 帮助模态框 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 15px;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.modal-header {
  padding: 25px;
  border-bottom: 1px solid #ecf0f1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 1.3rem;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #7f8c8d;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #ecf0f1;
  color: #2c3e50;
}

.modal-body {
  padding: 25px;
}

.help-section {
  margin-bottom: 25px;
}

.help-section h4 {
  color: #2c3e50;
  margin-bottom: 15px;
  font-size: 1.1rem;
}

.help-section ul {
  list-style: none;
  padding: 0;
}

.help-section li {
  padding: 8px 0;
  color: #34495e;
  line-height: 1.5;
  position: relative;
  padding-left: 20px;
}

.help-section li::before {
  content: '•';
  color: #3498db;
  font-weight: bold;
  position: absolute;
  left: 0;
}

.help-section li strong {
  color: #2c3e50;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .service-layout {
    grid-template-columns: 350px 1fr;
  }
}

@media (max-width: 768px) {
  .service-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  
  .prompt-panel {
    max-height: 300px;
  }
  
  .page-title {
    font-size: 2rem;
  }
  
  .customer-service-container {
    padding: 10px;
  }
}
</style>
